home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / dns / lamer / lamers.sh < prev    next >
Encoding:
Linux/UNIX/POSIX Shell Script  |  1992-06-25  |  6.9 KB  |  208 lines

  1. #!/bin/sh
  2. PATH=:/bin:/usr/bin:/usr/ucb:/usr/local/bin
  3. LOGFILE=/usr/spool/log/named
  4. MAILMSG=/usr/tmp/mailmsg$$
  5. LAMERS=/usr/tmp/lamers$$
  6. MSGFILE=/usr/local/bin/lamer-message
  7. LAMEREPORT=/tmp/.lamereport$$
  8. WEEKFILE=/usr/tmp/week$$
  9.  
  10. # -------------------------------------------------------------
  11. #  Copyright (c) 1991 Regents of the University of Michigan.
  12. #  All rights reserved.
  13. #
  14. #  Redistribution and use is permitted provided that this notice 
  15. #  is preserved and that due credit is given to the University of 
  16. #  Michigan. The name of the University may not be used to endorse 
  17. #  or promote products derived from this software without specific 
  18. #  prior written permission. This software is provided "as is" 
  19. #  without express or implied warranty.
  20. #
  21. #  Lame delegation notifier
  22. #  Author:  Bryan Beecher
  23. #  Last Modified:   6/25/92
  24. #
  25. #  To make use of this software, you need to be running the
  26. #  University of Michigan release of BIND 4.8.3, or any version
  27. #  of named that supports the LAME_DELEGATION patches posted to
  28. #  USENET.  The U-M release is available via anonymous ftp from
  29. #  terminator.cc.umich.edu:/unix/dns/bind4.8.3.tar.Z.
  30. #
  31. #  You must also have a copy of query(1) and host(1).  These
  32. #  are also available via anonymous ftp in the aforementioned
  33. #  place.
  34. # -------------------------------------------------------------
  35.  
  36. # -------------------------------------------------------------
  37. #  handle arguments
  38. # -------------------------------------------------------------
  39. #    -d <day>
  40. #    This flag is used to append a dot-day suffix to the LOGFILE.
  41. #    Handy where log files are kept around for the last week
  42. #    and contain a day suffix.
  43. #
  44. #    -f <logfile>
  45. #    Change the LOGFILE value altogether.
  46. #
  47. #    -w
  48. #    Count up all of the DNS statistics for the whole week.
  49. #
  50. #    -v
  51. #    Be verbose.
  52. #
  53. #    -t
  54. #    Test mode.  Do not send mail to the lame delegation
  55. #    hostmasters.
  56. # -------------------------------------------------------------
  57. VERBOSE=0
  58. TESTMODE=0
  59. while [ $# != 0 ] ; do
  60.     case "$1" in
  61.         -d)
  62.         LOGFILE=$LOGFILE"."$2
  63.         shift
  64.         ;;
  65.  
  66.         -f)
  67.         LOGFILE=$2
  68.         shift
  69.         ;;
  70.  
  71.         -w)
  72.         cat $LOGFILE* > $WEEKFILE
  73.         LOGFILE=$WEEKFILE
  74.         ;;
  75.  
  76.         -v)
  77.         VERBOSE=1
  78.         ;;
  79.  
  80.         -t)
  81.         TESTMODE=1
  82.         ;;
  83.     esac
  84.     shift
  85. done
  86.  
  87. #--------------------------------------------------------------------------
  88. #  Clean up and exit on a HUP, INT or QUIT
  89. #--------------------------------------------------------------------------
  90. trap "rm -f $LAMERS $MAILMSG $LAMEREPORT $WEEKFILE ; exit" 1 2 3
  91.  
  92. #--------------------------------------------------------------------------
  93. #  See if there are any lamers
  94. #--------------------------------------------------------------------------
  95. grep "Lame" $LOGFILE | tr A-Z a-z | grep -v "*" | awk '{
  96.     print substr($16, 2, length($16) - 3), $12 }' |
  97.     sort | uniq | awk '{
  98.         printf("%s %s\n", $1, $2)
  99. }' > $LAMERS
  100.  
  101. if [ ! -s $LAMERS ] ; then
  102.     exit 0
  103. fi
  104.  
  105. if [ $VERBOSE -eq 1 ] ; then
  106.     echo "Found" `awk 'END { print NR }' $LAMERS` "lame delegations"
  107. fi
  108.  
  109. #--------------------------------------------------------------------------
  110. #  There were lamers; send them mail
  111. #--------------------------------------------------------------------------
  112. touch $LAMEREPORT
  113. NAME=""
  114. while read DOMAIN IPADDR ; do
  115.     #-----------------------------------------------------------
  116.     # Echo args if verbose
  117.     #-----------------------------------------------------------
  118.     if [ $VERBOSE -eq 1 ] ; then
  119.         echo "$IPADDR may be a lame delegation for $DOMAIN"
  120.     fi
  121.     #-----------------------------------------------------------
  122.     # Lookup the SOA record form $DOMAIN.  A really broken name
  123.     # server many have more than one SOA for a domain, so exit
  124.     # after finding the first one.  Send it to the local hostmaster
  125.     # if we cannot find the proper one.
  126.     #-----------------------------------------------------------
  127.     if [ $VERBOSE -eq 1 ] ; then
  128.         echo "Looking up the hostmaster for $DOMAIN"
  129.     fi
  130.     HOSTMASTER=`query -h $DOMAIN -t SOA 2> /dev/null | awk '/mail addr/ { print $4 ; exit }' | sed -e 's/\./@/'`
  131.     NAME=`host $IPADDR 2> /dev/null`
  132.     if [ -z ""$HOSTMASTER ] ; then
  133.         if [ -z ""$NAME ] ; then
  134.             HOSTMASTER="hostmaster"
  135.         else
  136.             HOSTMASTER="postmaster@"$NAME
  137.         fi
  138.     fi
  139.     #-----------------------------------------------------------
  140.     # Find the name associated with IP address $IPADDR.  Query
  141.     # the nameserver at that address:  If it responds listing
  142.     # itself as a domain namserver, then it is lame; if it isn't
  143.     # in the list, then perhaps the lame delegation alert was
  144.     # spurious.
  145.     #-----------------------------------------------------------
  146.     if [ $VERBOSE -eq 1 ] ; then
  147.         echo "Making sure that $IPADDR is listed as a NS for $DOMAIN"
  148.     fi
  149.     if [ -n ""$NAME ] ; then
  150.         query -n $IPADDR -h $DOMAIN 2>&1 | grep "domain name" | grep -i $NAME > /dev/null
  151.         if [ $? -eq 1 -a $VERBOSE -eq 1 ] ; then
  152.             echo $NAME does not seem to be a nameserver for $DOMAIN
  153.             continue
  154.         fi
  155.     fi
  156.     #-----------------------------------------------------------
  157.     # If the delegation is no longer lame, don't send mail.
  158.     # We do the query twice; the first answer could be authori-
  159.     # tative even if the nameserver is not performing service
  160.     # for the domain.  If this is the case, then the second
  161.     # query will come from cached data, and will be exposed
  162.     # on the second query.  If the resolver returns trash, the
  163.     # entire set of flags will be set.  In this case, don't
  164.     # count the answer as authoritative.
  165.     #-----------------------------------------------------------
  166.     if [ $VERBOSE -eq 1 ] ; then
  167.         echo "Making sure that $IPADDR is not providing authoritative data now"
  168.     fi
  169.     query -n $IPADDR -h $DOMAIN > /dev/null 2>&1 
  170.     query -n $IPADDR -h $DOMAIN 2>&1 | grep header | grep aa | grep -v tc > /dev/null
  171.     if [ $? -eq 0 ] ; then
  172.         if [ $VERBOSE -eq 1 ] ; then
  173.             if [ -n ""$NAME ] ; then
  174.                 echo $NAME seems to be serving $DOMAIN OK now
  175.             else
  176.                 echo $I seems to be serving $DOMAIN OK now
  177.             fi
  178.         fi
  179.         continue
  180.     fi
  181.     #-----------------------------------------------------------
  182.     # Notify the owner of the lame delegation, and also notify
  183.     # the local hostmaster.
  184.     #-----------------------------------------------------------
  185.     if [ $TESTMODE -eq 0 ] ; then
  186.         if [ $VERBOSE -eq 1 ] ; then
  187.             echo "Sending mail to $HOSTMASTER about lame server $IPADDR for domain $DOMAIN"
  188.         fi
  189.         echo "To: " $HOSTMASTER > $MAILMSG
  190.         echo "Subject: $IPADDR appears to be a lame delegation for $DOMAIN" >> $MAILMSG
  191.         cat $MSGFILE >> $MAILMSG
  192.         sed -e "s|%DOMAIN%|$DOMAIN|" -e "s|%SERVER%|$IPADDR|" $MSGFILE |
  193.             /usr/lib/sendmail -t -fdns-maintenance
  194.     fi
  195.     echo $HOSTMASTER $DOMAIN $IPADDR >> $LAMEREPORT
  196. done < $LAMERS
  197. #--------------------------------------------------------------------------
  198. # No news is good news
  199. #--------------------------------------------------------------------------
  200. if [ -s $LAMEREPORT ] ; then
  201.     Mail -s "Lame report" hostmaster@umich.edu < $LAMEREPORT
  202. fi
  203.  
  204. #--------------------------------------------------------------------------
  205. # Tidy up
  206. #--------------------------------------------------------------------------
  207. rm -f $LAMERS $MAILMSG $LAMEREPORT $WEEKFILE
  208.